capt log close _all
log using   "${logs}FigureA3" , replace text
*------------

// Time to run: around 20 minutes

*==================================================================
* Figure A.3: Road tolls and bus lance distances, by year. 2008-2017
*==================================================================

use    "${dataout}MainDataset" , clear

keep if couple==1
keep if year>=2008 & year<=2017

label variable toll_fam_mean "Road toll (NOK)" 
label variable ptl_fam_km_mean "Bus lane (km)"


program cefhist
syntax varlist , xrange(string) yrange(string) ygrid(real) c(real) xlab(string) [addgraph(string)] [at(string)] *
tokenize `varlist'
local yvar `1'
local xvar `2'
local lineoptions lpattern(solid) lcolor(red)
quietly {
	tempvar x xnew ynew
	g `xnew' = .
	g `ynew' = .
	g `x'    = round(`yvar', `ygrid')
	bysort `x' : replace `x' = . if _n>1

	levelsof `xvar'
	if ("`at'"=="") local at `r(levels)'
	foreach e of num `r(levels)' {
		sum `yvar' if `xvar'==`e'
		replace `ynew' = r(mean) in `e'
		replace `xnew' = `e' in `e'
	}
	
	tempvar r y
	g `y'    = round(`yvar', `ygrid')
	bys `xvar' `y': g `r' = _N 
	bys `xvar': replace `r' = `r'/_N
	bys `xvar' `y': replace `r' = . if _n>1
	foreach e of num `at' {
		tempvar x`e' y`e' y`e'_0
		g `x`e'' = `y' if `xvar'==`e'
		g `y`e'' = `r' if `xvar'==`e'
		sum `y`e''
		replace `y`e'' = `e' + `y`e''/(r(max)*`c')
		g `y`e'_0' = `e'
		local gline `gline' (rbar `y`e'' `y`e'_0' `x`e'' if inrange(`x`e'',`yrange'), barwidth(`ygrid') horizontal lcol(black) lwidth(thin) fcol(ltbluishgray))
	}

	noi twoway `gline' (con `ynew' `xnew' if inrange(`xvar',`xrange'), sort ms(O) lpat(shortdash) mcol(red) lwidth(medthick)) `addgraph', ///
		legend(off) plotregion(margin(zero))  graphregion(color(white))  xlab(`xlab') `options'

	*gr export cefhist_`yvar'_`xvar'.eps, replace
}
end


*=========================================================================================
// 2) Histogram+time development in road toll/ptl over time
*=========================================================================================


// 2008-2017
cefhist toll_fam_mean year , xrange(2008,2017) yrange(0,40) ygrid(2) c(1) xlab(2008(1)2017) ytitle("Road toll (NOK)") scale(1.3)
graph export  "${figures}FigureA3a.png" , replace
graph export  "${figures}FigureA3a.pdf" , replace

cefhist ptl_fam_km_mean year , xrange(2008,2017) yrange(0,1) ygrid(0.1) c(1) xlab(2008(1)2017) ytitle("Bus lane (km)") scale(1.3)
graph export  "${figures}FigureA3b.png" , replace
graph export  "${figures}FigureA3b.pdf" , replace


// 2015-2017
preserve
keep if year>=2015 & year<=2017 

cefhist toll_fam_mean year , xrange(2015,2017) yrange(0,40) ygrid(2) c(1) xlab(2015(1)2017) ytitle("Road toll (NOK)") scale(1.3)
graph export  "${figures}FigureA3c.png" , replace
graph export  "${figures}FigureA3c.pdf" , replace

cefhist ptl_fam_km_mean year , xrange(2015,2017) yrange(0,1) ygrid(0.1) c(1) xlab(2015(1)2017)  ytitle("Bus lane (km)") scale(1.3)
graph export  "${figures}FigureA3d.png" , replace
graph export  "${figures}FigureA3d.pdf" , replace
restore

*------------
log close
